package Tests;

import static org.junit.Assert.*;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.hyperic.sigar.*;

public class Performance {

	IndexingProxy testedClass;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception 
	{
		testedClass = new IndexingProxy();
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testAddFile()
	{
		testedClass.addFile(null);
		
		if(testedClass.selectCurrentFile() != null)
		{
			fail("adding a null parametrizesd file should not work");
		}
	}
	
	@Test
	public void testRemoveFile()
	{
		File realFile = new File(""); //Add a legit file here, same as one from Addfile test
		
		testedClass.removeFile(null); //How do we check this?!
		
		testedClass.removeFile(new File("")); //How do we check this?!
		
		testedClass.removeFile(realFile); //How do we check this?!
		
	}
	
	@Test
	public void testIndexingProxy() 
	{
		int i;
		File testedFile = new File("path");
		
		for(i=0;i<=Integer.MAX_VALUE;i++)
		{
			testedClass.addFile(testedFile);
			
			testedClass.selectCurrentFile();
			
			if(testedClass.selectCurrentFile() == null)
			{
				fail("Where's the file?!");
			}
		}
		
		for(i=0;i<=Integer.MAX_VALUE;i++)
		{
			removeFile();
		}
		
		
		fail("Not yet implemented");
	}
	
	@Test
	public void testFileManager()
	{
		int i;
		
		for(i=0;i<=Integer.MAX_VALUE;i++)
		{
			testedClass.addFile();
		}
		
		for(i=0;i<=Integer.MAX_VALUE;i++)
		{
			testedClass.removeFile();
		}
	}
	
	@Test
	public void testHighIndex()
	{
		File directory = new File("path");
		
		addFile(directory);
	}
	
	@Test
	public void testUnknownFile() // nu se stie inca numarul de fisiere ce vor fi testate AICI 
	{
		File TestedFile = new File("path");
		
		addFile(TestedFile); 
	}

	@Test
	public void testCompatibilityHiddenFiles()
	{
		File TestedFile = new File("path");
		
		addFile(TestedFile);
		
		if(testedClass.selectCurrentFile().getName() != TestedFile.getName())
		{
			fail("Incompatbility with current hidden file!");
		}
	}
	
	@Test
	public void testFilesThatCanBeRead()
	{
		String version1, version2;
		
		File file1 = new File("path");
		
		addFile(file1);
		
		version1 = selectCurrentFile(file1).getVersion();
		
		BufferedWriter out = null;
		try  
		{
		    FileWriter fstream = new FileWriter("path", true); //true tells to append data.
		    out = new BufferedWriter(fstream);
		    out.write("\nANDRA");
		}
		catch (IOException e)
		{
		    System.err.println("Error: " + e.getMessage());
		}
		finally
		{
		    if(out != null) {
		        out.close();
		    }
		}
		
		addFile(file1);
		
		version2 = selectCurrentfile(file1).getVersion();
		
		if(version1 == version2) 
			fail("Test failed! The application did not recognize the modification!");
		
	}
	
	@Test
	public void testFilesOver1GB()
	{
		File file1 = new File("path");
		
		addFile(file1);
	}
	
	/* This one does not work like this, revise!
	@Test
	public void testIfAppInterferesWithTheFiles()
	{
		String version1, version2; 
		File file1 = new File("path");
		
		version1 = selectCurrentfile(file1).getVersion();
		
		addFile(file1);
		
		version2 = selectCurrentfile(file1).getVersion();
		
		if(version1 != version2) 
			fail("Test failed! The application does interfere");
		
	}
	*/
	
	@Test
	public void testLoadOnMachine()
	{
		Sigar machine = new Sigar();
		
		System.out.println(machine.getCpuInfoList()+" "
							+machine.getDiskUsage("D:/")+" "
							+machine.getLoadAverage()+" "
							+machine.getSwap());
		int i;
		for(i=0;i<1000;i++)
		{
			testedClass.addFile("");
			testedClass.getVersionList();
			testedClass.selectCurrentFile("");
			
			System.out.println(machine.getCpuInfoList()+" "
					+machine.getDiskUsage("D:/")+" "
					+machine.getLoadAverage()+" "
					+machine.getSwap());
		}
	}
	
	public void test_add_xml()
	{
	    try 
	    { 
	        File file = new addFile(path/file.xml);
	        fail("error");
	    } 
	    catch (Exception e)
	    {
	        assertTrue(e.hasSomeFlag());
	    }
	}
	
	public void test_add_docx()
	{
	    try 
	    { 
	        File file = new addFile(path/file.docx);
	        fail("error");
	    } 
	    catch (Exception e)
	    {
	        assertTrue(e.hasSomeFlag());
	    }
	}
 
	public void test_add_html()
	{
	    try 
	    { 
	        File file = new addFile(path/file.html);
	        fail("error");
	    } 
	    catch (Exception e)
	    {
	        assertTrue(e.hasSomeFlag());
	    }
	}
	
    public boolean pathAccessible(String path) 
    {
        File file = new File(path);
        if (!file.isDirectory())
               file = file.getParentFile();
        return file.exists();
    }

	public void test() 
	{
		test_hidden test = new test_hidden();
	    assertTrue(test.pathAccessible("regular file path"));
	    assertFalse(test.pathAccessible("hidden file path"));

	}
	
}
